package com.peanut.user.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

@Configuration
@ConfigurationProperties(prefix="user.redis")
public class JedisConfig {
	//连接节点信息 List<String> {"10.9.104.184:6379"
	//"10.9.104.184:6380","10.9.104.184:6381"}
	//easymall.redis.nodes 自动识别","截取赋值
	private List<String> nodes;
	//最大连接数 200
	private Integer maxTotal;
	//最大空闲8
	private Integer maxIdle;//easymall.redis.maxIdel
	//最小空闲3
	private Integer minIdle;
	
	//初始化方法 实现ShardedJedisPool的创建
	@Bean
	public ShardedJedisPool initJedisSPool(){
		//收集节点信息 对nodes进行处理
		List<JedisShardInfo> info=
			new ArrayList<JedisShardInfo>();
		//循环完成多次,nodes属性中携带的所有节点
		//都封装到了list中
		for(String node:nodes){
			//node=10.9.104.184:6379
			String host=node.split(":")[0];
			int port=Integer.parseInt(node.split(":")[1]);
			info.add(new JedisShardInfo(host,port));
		}
		//连接池的属性对象config
		
		return new ShardedJedisPool(initConfig(),info);
	}
	@Bean
	public JedisPoolConfig initConfig(){
		JedisPoolConfig config=new JedisPoolConfig();
		config.setMaxIdle(maxIdle);
		config.setMaxTotal(maxTotal);
		config.setMinIdle(minIdle);
		return config;
	}
	public List<String> getNodes() {
		return nodes;
	}
	public void setNodes(List<String> nodes) {
		this.nodes = nodes;
	}
	public Integer getMaxTotal() {
		return maxTotal;
	}
	public void setMaxTotal(Integer maxTotal) {
		this.maxTotal = maxTotal;
	}
	public Integer getMaxIdle() {
		return maxIdle;
	}
	public void setMaxIdle(Integer maxIdle) {
		this.maxIdle = maxIdle;
	}
	public Integer getMinIdle() {
		return minIdle;
	}
	public void setMinIdle(Integer minIdle) {
		this.minIdle = minIdle;
	}
	
}







